import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/16 9:43
 * @description 前缀和 当遍历到第i个元素时，设左边元素和为sum，那么右侧和为 total - sum - nums[i];
 * 则 sum = total - sum - nums[i]时符合条件
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public int pivotIndex(int[] nums) {
        int total = Arrays.stream(nums).sum();
        int sum = 0;
        for (int i = 0; i < nums.length; ++i) {
            if (2 * sum + nums[i] == total) {
                return i;
            }
            sum += nums[i];
        }
        return -1;
    }

}
